package com.xyyl.patient.controller;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.xyyl.patient.common.utils.CalculateAgeUtils;
import com.xyyl.patient.core.controller.BaseController;
import com.xyyl.patient.core.support.Pager;
import com.xyyl.patient.core.util.DateUtils;
import com.xyyl.patient.core.util.ResponseBodyResult;
import com.xyyl.patient.entity.archives.Archives;
import com.xyyl.patient.entity.user.PatientDiagnostic;
import com.xyyl.patient.service.user.PatientDiagnosticService;


 /**
 * @ClassName: PatientDiagnostic
 * @Description:PatientDiagnostic
 * @author @author wqs
 * @date 2018-04-03 10:00:51
 */

@RestController
@RequestMapping("/patientdiagnostic")
public class PatientDiagnosticController extends BaseController  {

	private Logger logger=LogManager.getLogger(PatientDiagnosticController.class);

	@Autowired
	private PatientDiagnosticService patientDiagnosticService;

	/**
	 * @Title: insert
	 * @Description: 用户新增既往病史
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insert(PatientDiagnostic patientDiagnostic) {

		logger.info("access method {}","insert");
		logger.info("patientDiagnostic",patientDiagnostic);
		try {
			if (patientDiagnostic.getArchivesId() == null) {
				return ResponseBodyResult.success("不正确",0);
			}
			return ResponseBodyResult.success("提交成功", patientDiagnosticService.insert(patientDiagnostic));

		} catch (Exception e) {
			return ResponseBodyResult.error("提交失败!", e);

		}
	}

	/**
	 * @Title: delete
	 * @Description: 用户删除既往病史（逻辑删除）
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject delete(@RequestParam Integer pdId) {

		logger.info("access method {}","delete");
		logger.info("pdId",pdId);
		try {
			int result = patientDiagnosticService.delete(pdId);
			if (result>0) {
				return ResponseBodyResult.success("删除成功", result);
			}else {
				return ResponseBodyResult.success("删除失败", result);
			}
		} catch (Exception e) {
			return ResponseBodyResult.error("删除失败!", e);

		}
	}

	/**
	 * @Title: update
	 * @Description: 用户修改既往病史
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject update(PatientDiagnostic patientDiagnostic) {

		logger.info("access method {}","update");
		logger.info("PatientDiagnostic",patientDiagnostic);
		try {
			// 如果主键ID为空 返回0
			if (null == patientDiagnostic.getId()) {
				return ResponseBodyResult.fail("ID为空，更新失败");
			}
			int result = patientDiagnosticService.update(patientDiagnostic);
			if (result>0) {
				return ResponseBodyResult.success("更新成功", result);
			} else {
				return ResponseBodyResult.success("更新失败", result);
			}
		} catch (Exception e) {
			return ResponseBodyResult.error("更新失败!", e);

		}
	}

	/**
	 * @Title: listpage
	 * @Description: 用户既往病史列表
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/listpage", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject listpage(@RequestParam Integer archivesId) {
		logger.info("access method {}","listpage");
		try {
			List<PatientDiagnostic> list = patientDiagnosticService.queryList(archivesId);
			return ResponseBodyResult.success("查询成功!", list);
		} catch (Exception e) {
			return ResponseBodyResult.error("查询失败!", e);

		}
	}

	/**
	 * @Title: get
	 * @Description: 用户既往病史详情
	 * @param @return 参数
	 * @return JSONObject 返回类型
	 * @throws
	 */
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject get(@RequestParam Integer pdId) {
		logger.info("access method {}","get");
		logger.info("pdId",pdId);
		try {
			 PatientDiagnostic PatientDiagnostic = patientDiagnosticService.queryById(pdId);
			 if (null != PatientDiagnostic) {
				 return ResponseBodyResult.success("查询成功!", PatientDiagnostic);
			} else {
				return ResponseBodyResult.success("查询失败!","记录不存在或已被删");
			}

		} catch (Exception e) {
			return ResponseBodyResult.error("查询失败!", e);

		}
	}
}
